package com.fy.controller;

import com.fy.dao.Mu_infoMapper;
import com.fy.dao.MuseumMapper;
import com.fy.entity.Mu_info;
import com.fy.entity.User_info;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/muinfo")
public class Mu_infoController {
    String id = "1";
    @Resource
    Mu_infoMapper mu_infoMapper;
    @Resource
    MuseumMapper museumMapper;

    @GetMapping("/getAll")
    public List<Mu_info> getAll() {
        return mu_infoMapper.findAll();
    }

    @PostMapping("/insert")
    public String insert(String mu_id, String mu_pic,
                      String mu_name, String mu_add,
                      String mu_info) {
        List<Mu_info> all = mu_infoMapper.findAll();
        for (Mu_info m : all) {
            if (m.getMu_name().equals(mu_name)) {
                return "406";//博物馆名字重复
            }
        }
        mu_infoMapper.insert(id, mu_pic, mu_name, mu_add, mu_info);
        museumMapper.insert(id, id);//自动调用
        id=(Integer.valueOf(id)+1+"");
        return "200";//创建博物馆成功

    }

    @PostMapping("/updateinfo")
    public String updateinfo(String mu_pic, String mu_name,
                          String mu_add, String mu_info,
                          String mu_id) {
        List<Mu_info> all = mu_infoMapper.findAll();
        for (Mu_info u : all) {
            if (mu_pic == null) {
                if (u.getMu_id().equals(mu_id)) {
                    mu_pic = u.getMu_pic();
                }
            }
            if (mu_name == null) {
                if (u.getMu_id().equals(mu_id)) {
                    mu_name = u.getMu_name();
                }
            }
            if (mu_add == null) {
                if (u.getMu_id().equals(mu_id)) {
                    mu_add = u.getMu_add();
                }
            }
            if (mu_info == null) {
                if (u.getMu_id().equals(mu_id)) {
                    mu_info = u.getMu_info();
                }
            }

        }
        mu_infoMapper.updateinfo(mu_pic, mu_name, mu_add, mu_info, mu_id);
        return "200";//修改博物馆信息成功

    }


}
